/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.dialog;

import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.vertexnet.realestate.interfaces.DefaultDialog;
import com.vertexnet.realestate.utility.PersistanceUtility;
import com.vertexnet.realestatenet.model.bean.Application;

/**
 *
 * @author Srivathsa
 */
public class ApplicationDialog extends DefaultDialog {

	private static final long serialVersionUID = 1L;
	private Application application;
	/**
     * Creates new form ApplicationDialog
     */
    public ApplicationDialog(Application application) {
        initComponents();
        this.application = application;
        if(application.getApplication() != null) {
        	applicationLabel.setIcon((Icon) PersistanceUtility.getObject(application.getApplication()));
        } else {
        	applicationLabel.setText("Please Click on Change/Add to add Photo");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        applicationLabel = new javax.swing.JLabel();
        changeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        applicationLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        changeButton.setText("Change/Add");
        changeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(applicationLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(changeButton)
                        .addGap(0, 599, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(applicationLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(changeButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed
    	try {
	    	File file = PersistanceUtility.getFile(this);
	    	if(file == null) {
	    		return;
	    	}
	    	InputStream is = new BufferedInputStream(new FileInputStream(file));
	    	Image resizeImage = PersistanceUtility.createResizedCopy(ImageIO.read(is), applicationLabel.getWidth(), applicationLabel.getHeight(), true);
	    	applicationLabel.setIcon(new ImageIcon(resizeImage));
	    	application.setApplication(PersistanceUtility.getBytes(applicationLabel.getIcon()));
		} catch (IOException e) {
			e.printStackTrace();
		}
    }//GEN-LAST:event_changeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel applicationLabel;
    private javax.swing.JButton changeButton;
    // End of variables declaration//GEN-END:variables
	@Override
	public void setupDialog() {
		// TODO Auto-generated method stub
		
	}
}
